'''
# !envs/tensorflow_GPU/python3
@Date    : 2018-03-29 13:41:45
@Author  : zhangcaocao (zhangcaocao66@gmail.com)
@Website : http://little-rocket.cn/ 
'''

import numpy as np
import matplotlib.pyplot as plt
import sympy
import platform
import cv2 as cv
import time


Points_List = []
Points_List_ = []

Bx = []
By = []

# 输出的点
Bx_ = []
By_ = []

# 可视化的点
Bx__ = []
By__ = []

k = len(Points_List)


def Help():
    print('python_version: '.format(platform.python_version()))


class Get_Point_:
    # 使用OpenCv对图像进行初步的处理

    # 使用 plt获取鼠标点击的坐标
    def Get_Pos(src_img):
        plt.imshow(src_img, cmap=plt.get_cmap("gray"))
        pos = plt.ginput(4)
        return pos

    def Get_Point():
        return Points_List_

    def Get_Image():
        src_img = cv.imread('./img/changdi2.png')
        src_Gray_Image = cv.cvtColor(src_img, cv.COLOR_BGR2GRAY)
        ret, dst_Image = cv.threshold(src_Gray_Image, 98, 255, cv.THRESH_BINARY)
        return dst_Image


class Draw_Bezier:



    # 解算Bezier方程的系数
    def Build_Bezier(Point_0, Point_1, Point_2, Point_3, t):
        A_0 = (1 - t) ** 3
        A_1 = ((1 - t) ** 2) * 3 * t
        A_2 = (t ** 2) * (1 - t) * 3
        A_3 = t ** 3

        x = Point_0[0] * A_0 + Point_1[0] * A_1 + Point_2[0] * A_2 + Point_3[0] * A_3
        y = Point_0[1] * A_0 + Point_1[1] * A_1 + Point_2[1] * A_2 + Point_3[1] * A_3
        return [int(x - 440), int(y)]  # 坐标转换

    # 绘图
    # noinspection PyTypeChecker
    def Draw(Points_List):
        # 起点（440， 240），总的（465， 456）;
        # 生成的点
        for t in range(0, 100):
            Point_Bulid_Bezier = Draw_Bezier.Build_Bezier(Points_List[0], Points_List[1], Points_List[2],
                                                          Points_List[3], t / 100)

            Points_List_.append(Point_Bulid_Bezier)

            # 绘图点
            Bx__.append(Points_List_[t - 1][0] + 440)
            By__.append(Points_List_[t - 1][1])

            # 实际场地坐标
            Bx_.append(Points_List_[t - 1][0])
            By_.append(Points_List_[t - 1][1])

        # 选择的点
        for j in range(len(Points_List)):
            Bx.append(Points_List[j][0])
            By.append(Points_List[j][1])

        print(Points_List_)

        plt.figure(1)
        plt.scatter(Bx, By, marker='x', s=100, c='red')
        plt.plot(Bx, By, c='r')  # 未拟合
        plt.plot(Bx__, By__, c='g')  # 拟合后
        plt.savefig("./img/Figure_1.png")
        plt.figure(2)
        plt.plot(Bx_, By_, c='b')


def Save_File(Points):
    fo = open("Point.h", "w")
    for k in range(len(Points)):
        fo.write(str(Points[k]) + '\n')
    fo.close()
